<template>
  <div class="interface-group">

    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="接口分组" name="first">
        <el-button-group style="margin-bottom: 5px;">
          <el-button type="primary" size="small" @click="addEditGroup()"><i class="el-icon-plus"></i>新建</el-button>
          <el-button type="success" size="small" @click="getAPiGroup(projectId)"><i class="el-icon-refresh"></i>刷新</el-button>
        </el-button-group>
      </el-tab-pane>
      <el-input placeholder="输入分组名称进行过滤" v-model="filterText"></el-input>
      <!--分组列表树形图-->
      <el-tree ref="tree" :data="interfaceGroupList" :props="defaultProps" :filter-node-method="filterNode" @node-click="handleNodeClick">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span><i class="el-icon-folder-opened"></i> {{ node.label }}</span>
          <span style="padding-left:15px">
            <i class="el-icon-edit" @click="clickEditGroup(node,data)"></i>
            <i class="el-icon-delete" @click="deleteApiGroup(node, data)"></i>
          </span>
        </span>
      </el-tree>
    </el-tabs>
    <!--添加分组弹框-->
    <el-dialog :title='type' :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="interfaceGroupForm" label-width="80px">
        <el-form-item label="分组名称" required>
          <el-input v-model="interfaceGroupForm.name" placeholder="请输入分组名"></el-input>
        </el-form-item>
        <el-form-item label="分组描述">
          <el-input type="textarea" placeholder="请输入描述" v-model="interfaceGroupForm.description" :autosize="{ minRows: 4, maxRows: 10}">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="createApiGroup(interfaceGroupForm)" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {createGroup, deleteGroup, getGroupAll, editGroup} from '@/api/group'
import dateFormat from "@/utils/dateFormat";

export default {
  data() {
    return {
      activeName: 'first',
      projectId: this.$store.state.projectId,
      filterText: '',
      type: '添加分组',
      dialogVisible: false,
      interfaceGroupForm: {},
      interfaceGroupList: [],
      defaultProps: {
        // children: 'children',
        label: 'name'
      }
    }
  },

  //监听过滤输入框变化
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
    '$store.state.projectId'() {
      this.projectId = this.$store.state.projectId
      this.getAPiGroup(this.projectId)
    }
  },

  methods: {
    addEditGroup() {
      this.type = '添加分组'
      this.interfaceGroupForm = {name: '', description: ''}
      this.dialogVisible = true
    },
    clickEditGroup(Node, data) {
      this.type = '编辑分组'
      this.dialogVisible = true
      this.interfaceGroupForm.name = data.name
      this.interfaceGroupForm.description = data.description
      this.interfaceGroupForm.id = data.id
    },
    //创建分组
    createApiGroup(data) {
      data.projectId = this.projectId
      if (data.name == null || data.name === '') {
        this.$message({
          message: '分组名不能为空！',
          type: 'warning'
        });
        return
      }
      if (this.type === '添加分组') {
        data.createTime = dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
        data.updateTime = dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
        createGroup(data).then(resp => {
          if (resp.code === 200) {
            this.$message({
              message: resp.message,
              type: 'success'
            });
            this.dialogVisible = false
            this.getAPiGroup(this.projectId)
          } else {
            this.$message({
              message: resp.message,
              type: 'warning'
            });
          }
        })
        return;
      }
      if (this.type === '编辑分组') {
        data.updateTime = dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
        editGroup(data).then(resp => {
          if (resp.code === 200) {
            this.$message({
              message: resp.message,
              type: 'success'
            });
            this.dialogVisible = false
            this.getAPiGroup(this.projectId)
          } else {
            this.$message({
              message: resp.message,
              type: 'warning'
            });
          }
        })
      }

    },
    //查询分组
    getAPiGroup(projectId) {
      this.interfaceGroupList = []
      getGroupAll(projectId).then(resp => {
        this.interfaceGroupList = resp.data
      })
    },
    //删除分组
    deleteApiGroup(node, data) {
      this.$confirm('此操作将永久删除该分组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGroup(data.id).then(resp => {
          if (resp.code === 200) {
            this.$message({
              type: 'success',
              message: resp.message
            });
            this.getAPiGroup(this.projectId)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //分组过滤
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick() {
    },
  },

  mounted() {

  },
}
</script>

<style scoped>
.interface-group {
  height: 100%;
  background-color: white;
}

.interface-group >>> .el-tabs__content {
  padding: 10px;
}

.interface-group >>> .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  color: #43a047;
  background-color: #ecf8ee;
}
</style>

<style>
.el-tree-node__label {
  font-size: 14px;
  font-family: "Helvetica Neue", serif;
}

.el-tree-node__children {
  font-size: 13px;
  font-family: "Helvetica Neue", serif;
}

.el-tree-node__content {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  height: 35px;
  cursor: pointer;
}

.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  color: #43a047;
  background-color: #ecf8ee;
}

.custom-tree-node {
  font-size: 14px;
}

.el-row, .el-col-5, .el-col-19{
  height: 100%;
}

</style>
